package com.clss.commandhelper.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.clss.commandhelper.R;
import com.clss.commandhelper.tools.Lg;
import com.clss.commandhelper.tools.eventbus.EventBusMessage;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.ButterKnife;

/**
 *
 * @author Liuv
 * @date 2017/4/12
 */

public abstract class BaseFragment extends Fragment implements BaseView {
    private static final String TAG = "BaseFragment";

    protected Toast mToast;
    protected ProgressDialog mProgressDialog;
    protected View rootView;
    protected BaseActivity mBaseActivity;

    protected abstract int setViewId();
    protected abstract void initView(View v);
    protected abstract void initData(Bundle savedInstanceState);

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(setViewId(),container, false);
            ButterKnife.bind(this, rootView);
            EventBus.getDefault().register(this);
            initView(rootView);
            initData(savedInstanceState);
        }
        ViewGroup parent = (ViewGroup)rootView.getParent();
        if(parent != null){
            parent.removeView(rootView);
        }
        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
        getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        Lg.i(TAG, "onPause");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onEventMainThread(EventBusMessage eventBusMessage) {
        Lg.i(TAG, "---onEventMainThread===" + eventBusMessage.getCode());
    }

    @Override
    public void showLoadingDialog() {
        showLoadingDialog(null, null);
    }

    @Override
    public void showLoadingDialog(String msg) {
        showLoadingDialog(null, msg);
    }

    public void showLoadingDialog(String title, String message){
        if (mProgressDialog == null){
            mProgressDialog = new ProgressDialog(getActivity());
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }
        if (title == null) {
            title = "";
        }
        if (TextUtils.isEmpty(message)) {
            message = getString(R.string.loading);
        }
        mProgressDialog.setTitle(title);
        mProgressDialog.setMessage(message);
        mProgressDialog.show();
    }



    @Override
    public void dismissLoadingDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()){
            mProgressDialog.dismiss();
        }
    }

    @Override
    public Context getActContext() {
        return getContext();
    }

    @Override
    public void showLongToast(int messageId) {
        showLongToast(getString(messageId));
    }

    @Override
    public void showLongToast(String message) {
        if (TextUtils.isEmpty(message)){
            return;
        }
        Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showShortToast(int messageId) {
        showShortToast(getString(messageId));
    }

    @Override
    public void showShortToast(String message) {
        if (TextUtils.isEmpty(message)){
            return;
        }
        Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
    }

    private Toast checkNullOrNew(){
        if (mToast == null){
            mToast = new Toast(getContext());
        }
        return mToast;
    }

    public void setActivity(BaseActivity activity){
        mBaseActivity = activity;
    }
}
